import datetime
from typing import List
from pydantic import Field, BaseModel
from package.fastapi.schema import Page, BatchId
from common.analyzer_common.scheme import Search


class _Query(Search):
    condition: dict = Field(..., title='搜索 聚合', 
                        example={"query": {"match_all": {}}, 
                                 "aggs": {"timeline": {"date_histogram": {"field": '_time','calendar_interval': '1d',"min_doc_count": 0}}}})


class SearchTimeline(_Query):
    """平台的搜索"""
    pass


class SearchHistory(BaseModel):
    search: Search = Field(..., title='搜索条件')
    created_at: datetime.datetime = Field(None, title='')

    class Config:
        orm_mode = True
        json_encoders = {datetime.datetime: lambda dt: int(dt.timestamp() * 1000)}


class PageSearchHistory(Page):
    """获取页码条目"""
    data: List[SearchHistory] = Field(..., title='')


class SearchFavorite(SearchHistory):
    name: str = Field(..., title='搜索名') 

    class Config:
        orm_mode = True
        json_encoders = {datetime.datetime: lambda dt: int(dt.timestamp() * 1000)}


class PageSearchFavorite(Page):
    """获取页码条目"""
    data: List[SearchFavorite] = Field(..., title='')
